﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web.UserControls
{
    public partial class SelectEmployer : System.Web.UI.UserControl
    {
        //public int SelectedUserID
        //{
        //    get
        //    {
        //        return Int32.Parse(RadComboBoxEmployee.SelectedValue);
        //    }
        //    set
        //    {
        //        RadComboBoxEmployee.SelectedValue = value.ToString();
        //    }
        //}

        public int? SelectedUserID { get; set; }


        public event EventHandler EventEmployeeChanged;
        //private List<User> allUsers;// = new List<User>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                
                PopulateEmployees(null);

                if (SelectedUserID != null)
                {
                    RadComboBoxEmployee.SelectedValue = SelectedUserID.ToString();
                }
                /////
                /*UserManager um = new UserManager();
            
                allUsers = (List<User>)um.GetAllUsers();
                PopulateEmployees2(allUsers);
                 * */
            }
        }

        private void PopulateEmployees2(List<User> users)
        {
            RadComboBoxEmployee.DataSource = users;
            
            RadComboBoxEmployee.DataBind();
        }

        private void PopulateEmployees(int? SBU_ID)
        {
            UserManager um = new UserManager();

            if (SBU_ID == null)
            {
                RadComboBoxEmployee.DataSource = um.GetAllUsers();
            }
            else
            {
                RadComboBoxEmployee.DataSource = um.GetAllUsersByDepartment((int)SBU_ID);
            }
            RadComboBoxEmployee.DataBind();
            //RadComboBoxEmployee.Enabled = true;
        }

        
        
        protected void RadComboBoxDepartment_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //RadComboBoxEmployee.Enabled = false;
            PopulateEmployees(Int32.Parse(RadComboBoxDepartment.SelectedValue));

            /*List<User> users;
            if (e.Value == "0")
            {
                users = allUsers;
            }
            else
            {
                users = allUsers.FindAll(u => u.SBU_FK == Int32.Parse(e.Value));
            }

            PopulateEmployees2(users);*/
        }

        protected void RadComboBoxEmployee_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            if (EventEmployeeChanged != null)
            {
                EventEmployeeChanged(sender, e);
            }
        }

        //public RadComboBoxItem GetSelectedDepartmentComboboxItem()
        //{
        //    return RadComboBoxDepartment.SelectedItem;
        //}

        //public User GetSelectedUser()
        //{
        //    return (User)RadComboBoxEmployee.SelectedItem.DataItem;
        //}

        //public void SetSelectedEmployee(int userID)
        //{
        //    RadComboBoxEmployee.SelectedValue = userID.ToString();
        //}
    }
}